package com.easy.controller;

import com.easy.bean.ConsumptionRecord;
import com.easy.service.ConsumptionRecordService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("consumptionRecord")
public class ConsumptionRecordController {

    @Autowired
    ConsumptionRecordService consumptionRecordService;

    @PostMapping
    public Result add(@RequestBody ConsumptionRecord consumptionRecord) {
        Object obj = consumptionRecordService.add(consumptionRecord);
        return Result.success(obj);
    }

    @PutMapping
    public Result edit(@RequestBody ConsumptionRecord consumptionRecord) {
        Object obj = consumptionRecordService.edit(consumptionRecord);
        return Result.success("编辑信息保持成功", obj);
    }

    // 查询今天各支付方式的收入占比
    @GetMapping("/todayIncomeByMethod")
    public Result todayIncomeByMethod() {
        try {
            List<Map<String, Object>> result = consumptionRecordService.todayIncomeByMethod();
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(500, "查询失败: " + e.getMessage());
        }
    }


    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id) {
        Object obj = consumptionRecordService.del(id);
        return Result.success("删除成功", obj);
    }

    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id) {
        Object obj = consumptionRecordService.one(id);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all() {
        Object obj = consumptionRecordService.all();
        return Result.success(obj);
    }

    @GetMapping("page")
    public Result page(Page page, ConsumptionRecord consumptionRecord) {
        // 查询一共有多少条数据
        // 当前页要显示的数据
        // 将以上数据封装到page对象中
        // 返回page对象
        consumptionRecordService.page(page, consumptionRecord);
        // 返回page对象
        return Result.success(page);
    }
    @GetMapping("incomeByDate")
    public Result incomeByDate(@RequestParam("date") String date) {
        return Result.success(consumptionRecordService.getIncomeByDate(date));
    }

    @GetMapping("todayIncome")
    public Result todayIncome() {
        return Result.success(consumptionRecordService.todayIncome());
    }
}
